package com.lsl.bhllserver.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsl.bhllserver.dao.func.IDelete;
import com.lsl.bhllserver.dao.func.IInsert;
import com.lsl.bhllserver.dao.func.IQuery;
import com.lsl.bhllserver.dao.func.IUpdate;
import com.lsl.bhllserver.dao.template.DAOTemplate;
import com.lsl.bhllserver.entity.*;
import com.lsl.bhllserver.mapper.*;

@Repository
public class UserDAO<R> implements DAOTemplate<R> {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    BusinessMapper businessMapper;

    private R typeflag;

    @SuppressWarnings("unchecked")
    public R findByID(String userid) {
        try {
            // QueryWrapper<R> wrapper = new QueryWrapper<>();
            // int uidInt = Integer.parseInt(keywords[0]);
            // wrapper.eq("id", uidInt);
            Integer uidInt = Integer.parseInt(userid);
            if (typeflag instanceof Admin) {
                Admin admin = adminMapper.selectById(uidInt);
                System.out.println(admin.toString());
                return (R) adminMapper.selectById(uidInt);
            } else if (typeflag instanceof Customer) {
                return (R) customerMapper.selectById(uidInt);
            } else if (typeflag instanceof Business) {
                return (R) businessMapper.selectById(uidInt);
            } else {
                System.out.println("+++++ 类型不对？？？？");
                return null;
            }
        } catch (Exception e) {
            System.out.println("+++++ findByID 方法出错：");
            e.printStackTrace();
            return null;
        }
    }

    public boolean insert(Object user) {
        IInsert insertor = (usertemp) -> {
            // if (usertemp instanceof Admin) {
            //     return adminMapper.insert((Admin) user) > 0;
            // } else if
            if (usertemp instanceof Customer) {
                return customerMapper.insert((Customer) user) > 0;
            } else if (usertemp instanceof Business) {
                return businessMapper.insert((Business) user) > 0;
            } else {
                System.out.println("+++++ 类型不对？？？？");
                return false;
            }
            //return true;
        };
        try{
            return insertor.insert(user);
        } catch (Exception e) {
            System.out.println("+++++ insert 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    public boolean update(Integer userid, Object user) {
        IUpdate updater = (uid, usertemp) -> {
            if (usertemp instanceof Admin) {
                Admin admintemp = (Admin) usertemp;
                admintemp.setId(userid);
                return adminMapper.updateById((Admin) admintemp) > 0;
            } else if (usertemp instanceof Customer) {
                Customer customertemp = (Customer) usertemp;
                customertemp.setId(userid);
                return customerMapper.updateById((Customer) customertemp) > 0;
            } else if (usertemp instanceof Business) {
                Business businesstemp = (Business) usertemp;
                businesstemp.setId(userid);
                return businessMapper.updateById((Business) businesstemp) > 0;
            } else {
                System.out.println("+++++ 类型不对？？？？");
                return false;
            }
            
        };
        try{
            return updater.update(userid, user);
        } catch (Exception e) {
            System.out.println("+++++ update 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    public boolean delete(Integer userid) {
        IDelete deletor = (uid) -> {
            if (typeflag instanceof Admin) {
                return adminMapper.deleteById(userid) > 0;
            } else if (typeflag instanceof Customer) {
                return customerMapper.deleteById(userid) > 0;
            } else if (typeflag instanceof Business) {
                return businessMapper.deleteById(userid) > 0;
            } else {
                System.out.println("+++++ 类型不对？？？？");
                return false;
            }
        };
        try {
            return deletor.delete(userid);
        } catch (Exception e) {
            System.out.println("+++++ delete 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    public List<R> findByKeywords(String[] keywords) {
        IQuery<R> queryor = (kw) -> {
            if (typeflag instanceof Customer) {
                QueryWrapper<Customer> wrapper = new QueryWrapper<>();
                wrapper.like("id", keywords[0])
                       .like("cus_name", keywords[1])
                       .like("cus_phone", keywords[2])
                       .like("cus_address", keywords[3]);
                return (List<R>) customerMapper.selectList((QueryWrapper<Customer>)wrapper);
            } else if (typeflag instanceof Business) {
                QueryWrapper<Business> wrapper = new QueryWrapper<>();
                wrapper.like("id", keywords[0])
                       .like("bus_name", keywords[1])
                       .like("bus_phone", keywords[2])
                       .like("bus_address", keywords[3]);
                return (List<R>) businessMapper.selectList((QueryWrapper<Business>)wrapper);
            } else {
                System.out.println("+++++ 类型不对？？？？");
                return null;
            }
        };
        try {
            return queryor.query(keywords);
        } catch (Exception e) {
            System.out.println("+++++ findByKeywords Lambda方法出错：");
            e.printStackTrace();
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public List<R> getAll() {
        IQuery<R> queryor = (kw) -> {
            if (typeflag instanceof Admin) {
                return (List<R>) adminMapper.selectList(null);
            } else if (typeflag instanceof Customer) {
                return (List<R>) customerMapper.selectList(null);
            } else if (typeflag instanceof Business) {
                return (List<R>) businessMapper.selectList(null);
            } else {
                System.out.println("+++++ 类型不对？？？？");
                return null;
            }
        };
        try {
            return queryor.query(null);
        } catch (Exception e) {
            System.out.println("+++++ findAll Lambda方法出错：");
            e.printStackTrace();
            return null;
        }
    }

    public R getTypeflag() {
        return typeflag;
    }

    public void setTypeflag(R typeflag) {
        this.typeflag = typeflag;
    }

    // @SuppressWarnings("unchecked")
    // public List<R> test() {
    // LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
    // return (List<R>)adminMapper.selectList(wrapper);
    // }
}
